java ImageIO。写入()低质量图像
我创建了以下方法,它构建了一个图像的base64编码字符串。我遇到的问题是,当这个运行时,它获取的图像是高质量的,但是当它保存到字节数组中,然后进行编码时,图像是像素化的,质量相当低,我该怎么做才能获得100%质量的图像
public String getImageString(String img){
String image = "";
try{
BufferedImage bufferedImage = ImageIO.read(HelpPage.class.getResource(img));
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "JPG", out);
String base64bytes = Base64.encode(out.toByteArray());
image = "data:image/jpeg;base64," + base64bytes;
}catch(IOException ex){
Logger.getLogger(HomePage.class.getName()).log(Level.SEVERE, null, ex);
}
return image;
}
# 1 楼答案
这里根本不需要使用ImageIO。只需从资源中读取字节,并对它们进行base64编码
您正在将一个JPEG转换为另一个JPEG,这是一个固有的有损过程,尽管它不应该像“低质量”那样糟糕。但你根本不需要这么做